package com.ziyia.wim.activity;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.ziyia.wim.pojo.Client;
import com.ziyia.wim.pojo.Msg;
import com.ziyia.wim.R;
import com.ziyia.wim.adapter.MsgAdapter;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Wim extends BaseActivity {
    private Client client;

    private EditText inputText;
    private Button send;
    private TextView title;
    protected MsgAdapter msgAdapter;
    protected RecyclerView recyclerView;
    private List<Msg> msgList = new ArrayList<>();
    private String id;
    private String username, group;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wim);


        title = findViewById(R.id.title);
        // 发送按钮
        send = findViewById(R.id.send);
        // 输入框控件
        inputText = findViewById(R.id.inputText);
        // 列表控件
        recyclerView = findViewById(R.id.recycleView);
        // 获取上一个页面的传递的用户名
        id = getIntent().getStringExtra("uid");
        group = getIntent().getStringExtra("group");
        username = getIntent().getStringExtra("username");


        title.setText(group);

        String url = "ws://" + IP + "/websocket/" + id;
        URI uri = URI.create(url);
        client = new Client(uri) {
            @Override
            public void onMessage(String message) {
                Map<String, String> map = JSON.parseObject(message, Map.class);
                String username = map.get("username");
                String content = map.get("content");
                Msg msg = new Msg(group, username, content, Msg.TYPE_RECEIVED);
                msgList.add(msg);
                msgAdapter.notifyItemInserted(msgList.size() - 1);
                recyclerView.smoothScrollToPosition(msgList.size() - 1);
            }
        };

        // 连接 Websocket
        client.connect();

        // 创建线性布局来显示
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        msgAdapter = new MsgAdapter(this, msgList);
        recyclerView.setAdapter(msgAdapter);
//        recyclerView.setItemAnimator(null);

        send.setOnClickListener(v -> {
            String value = inputText.getText().toString();
            send(value);
        });


    }


    private void send(String value) {
        // 获取消息框的内容
        // 如果消息框的内容不为空
        if (!value.isEmpty()) {
            // 封装消息
            Msg msg = new Msg(group, username, value, Msg.TYPE_SENT);

            String jsonString = JSON.toJSONString(msg);
            // 将封装的消息添加到消息列表
            msgList.add(msg);
            if (client.isOpen()) {
                client.send(jsonString);
            }
            if (msgAdapter != null) {
                msgAdapter.notifyItemInserted(msgList.size() - 1);
                recyclerView.scrollToPosition(msgList.size() - 1);
                // 设置消息框为空
                inputText.setText("");
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        client.close();
    }

}